﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Poligono.Web
{
    /// <summary>
    /// Class that contains the methods used to show messages in forms.
    /// </summary>
    public class WebData
    {
        private const string ID_WEB_MESSAGE = "CD756627-E315-4AC8-9C59-3F31FE8883A0";

        /// <summary>
        /// Method that places the object WebMessage in a session
        /// </summary>
        /// <param name="webMessage">WebMessage object passed as parameter</param>
        public static void AssignMessage(WebMessage webMessage)
        {
            HttpContext.Current.Session[ID_WEB_MESSAGE] = webMessage;
        }

        /// <summary>
        /// Overload method AssignMessage where instead of passing the object is passed the message and the same type.
        /// </summary>
        /// <param name="message">Message</param>
        /// <param name="messageType">Reports the type of message</param>
        public static void AssignMessage(string message, MessageType messageType)
        {
            WebMessage msg = new WebMessage(message, messageType);
            AssignMessage(msg);
        }

        /// <summary>
        /// Method that gets the message that is stored in session variable.
        /// </summary>
        /// <returns></returns>
        public static WebMessage GetMessage()
        {
            WebMessage msg = (WebMessage)HttpContext.Current.Session[ID_WEB_MESSAGE];
            HttpContext.Current.Session[ID_WEB_MESSAGE] = null;
            return msg;
        }

        /// <summary>
        /// Determine whether there is any message in session.
        /// </summary>
        /// <returns>True/False</returns>
        public static bool HasMessage()
        {
            return HttpContext.Current.Session[ID_WEB_MESSAGE] != null;
        }
    }
}